// import { log10 } from "core-js/core/number"
var {smarts} = require('./test')
function phone(str) {
    let phone = str.match(/\d+(\.\d+)?/g)
    phone.sort((a, b) => b.length - a.length)
    return phone[0]

}
const provinces = ['北京', '天津', '河北', '山西', '内蒙古', '辽宁', '吉林', '黑龙江', '上海', '江苏', '浙江', '安徽', '福建', '江西', '山东', '河南', '湖北', '湖南', '广东', '广西', '海南', '四川', '贵州', '云南', '重庆', '西藏', '陕西', '甘肃', '青海', '宁夏', '新疆', '香港', '澳门', '台湾']

function buildSearchAddress(items, provinceIdx) {
    if (provinceIdx == 0) {
        return items.map(it => it.str).join(" ")
    } else {
        let candidateUserInfos = items.slice(0, provinceIdx)
        let candidateAddressInfos = items.slice(provinceIdx)
        return candidateAddressInfos.concat(candidateUserInfos).map(it => it.str).join(" ")
    }
}
//过滤特殊字符
function stripscript(s) {
    s = s.replace(/(\d{3})-(\d{4})-(\d{4})/g, '$1$2$3');
    s = s.replace(/(\d{3}) (\d{4}) (\d{4})/g, '$1$2$3');
    var pattern = new RegExp(
        "[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“’。，、？-]"
    );
    var rs = '';
    for (var i = 0; i < s.length; i++) {
        rs = rs + s.substr(i, 1).replace(pattern, ' ');
    }
    rs = rs.replace(/[\r\n]/g, '');
    return rs;
}

function smart(event) {
    event = stripscript(event); //过滤特殊字符
    let copyaddress = JSON.parse(JSON.stringify(event));
    copyaddress = copyaddress.split(' ');

    return copyaddress;

}

//return [name, searchAddress]
function namesAndSearchAddress(str) {
    let result = smart(str)
        .filter((res) => res != "")
        .map((it, index) => ({ idx: index, str: it }))
    // console.log(result)
    
    let provinceIdx = -1
    let candidateProvFields = result.filter(it => provinces.some(p => it.str.indexOf(p) >= 0)).sort((v1, v2) => v1.length - v2.length)
    // console.log(candidateProvFields, "candidateProvFields");
    if (candidateProvFields.length > 0) {
        provinceIdx = candidateProvFields[0].idx
    }

    if (provinceIdx >= 0) {
        let candidateUserNames = result
            .filter(it => it.str.length <= 4)
            .filter(it => it.str.indexOf("街道") < 0)
            .filter(it => it.str.indexOf("县") != (it.str.length - 1))
            .filter(it => it.str.indexOf("区") != (it.str.length - 1))
            .filter(it => it.str.indexOf("市") != (it.str.length - 1))
        if (provinceIdx > 0) {
            let candidateNamesBeforeProvince = candidateUserNames.filter(it => it.idx < provinceIdx)
            if (candidateNamesBeforeProvince.length > 0) {
                return [candidateNamesBeforeProvince[0].str, buildSearchAddress(result, provinceIdx)]
            }
        }
        let candidateNamesBeforeProvince = candidateUserNames.filter(it => it.idx > provinceIdx)
        if (candidateNamesBeforeProvince.length > 0) {
            return [candidateNamesBeforeProvince[candidateNamesBeforeProvince.length - 1].str, buildSearchAddress(result, provinceIdx)]
        }
    }
    return [null, null];
}

export function sub(str){
    let news = smarts(str)
    let namesAndAdd = namesAndSearchAddress(str)
    let names = namesAndAdd[0]
    // let searchAddress = namesAndAdd[1]
    let olds = {
        phone:news.phone,
        names:names,
        province:news.province,
        city:news.city,
        county:news.county,
        // street:news.street,
        // searchAddress: searchAddress
    }
    let detal = str
    
    for(let obj in olds){
       if(olds[obj]){
        detal = detal.replace(olds[obj],'') 
       }
    }
    detal = stripscript(detal).replace(/\s+/g, "");
    olds.datal = detal
    return olds
}

// let namesAndAdd = namesAndSearchAddress('浙江省 杭州市 临平区 南苑街道   杭州临平区南苑街道高地工业区东湖南路587号4楼-杭州永腾服装-Amy-18969016655')
// console.log(smarts('浙江省 杭州市 临平区 南苑街道   杭州临平区南苑街道高地工业区东湖南路587号4楼-杭州永腾服装-Amy-18969016655'));
// console.log(sub('浙江省 杭州市 临平区 南苑街道   杭州临平区南苑街道高地工业区东湖南路587号4楼-杭州永腾服装-Amy-18969016655'));
